<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Role;
use App\Admin\Auth;
use DB;

class RoleController extends Controller
{
    /* 角色列表展示 */
    public function index(Request $request)
    {
        if ($request->expectsJson()) {
            //使用渴求式加载来减少数据库查询次数
            $data = DB::table('role')->where('id','>',1)->get();

            foreach($data as $k=> $item){
                $sql = "SELECT CONCAT(username,'（',realname,'）') as name,status from manager WHERE FIND_IN_SET(".$item->id.",role_ids) or role_id = ".$item->id;
                $managers = json_encode(DB::select($sql));
                $data[$k]->managers = $managers;
            }
            $jsonData['data'] = $data;
            return response()->json($jsonData);
        } else {
            return view('admin.role.index');
        }
    }

    /* 角色修改 */
    public function edit(Request $request)
    {
        $role = new Role();

        if ($request->isMethod('POST')) {
            $data = $request->only(['id', 'auth_id', 'role_name', 'remark']);
            // dd($data);
            //交给模型处理数据
            $result = $role->assignAuth($data);
            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result ? '1' : '0';
        } else {
            //查询被分配权限的角色信息
            $id = $request->input('id');
            $role_info = $role->find($id);
            if ($role_info) {
                //查询当前角色已经拥有的权限
                $have_auth_ids = $role_info->auth_ids; //例如 ： 101,103,104,107,109
                $have_auth_ids = explode(',', $have_auth_ids); //字符串 --》数组，使得判断更严谨

                //获得可供选取分配的权限信息
                if ($id == 50) {
                    # 角色为 注册客户
                    $auth_infoA = Auth::where('id', '=', '157')->get(); //父级
                } else {
                    # code...
                    $auth_infoA = Auth::where('level', '0')->where('auth_name', '<>', '')->orderBy('sort')->get(); //父级
                }

                $auth_infoB = Auth::where('level', '1')->where('auth_name', '<>', '')->orderBy('sort')->get(); //子级1
                $auth_infoC = Auth::where('level', '2')->where('auth_name', '<>', '')->orderBy('sort')->get(); //子级2

                return view('admin.role.edit', compact('role_info', 'have_auth_ids', 'auth_infoA', 'auth_infoB', 'auth_infoC'));
            } else {
                $error_title = '';
                $error_description = '当前记录已经删除 ~';
                return view('errors.error', compact('error_title', 'error_description'));
            }
        }
    }

    //角色添加
    public function add(Request $request)
    {
        $role = new Role();

        if ($request->isMethod('POST')) {
            $data = $request->only(['auth_id', 'role_name', 'remark']);
            //交给模型处理数据
            $result = $role->insertRole($data);
            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result ? '1' : '0';
        } else {
            //获得可供选取分配的权限信息
            $auth_infoA = Auth::where('level', '0')->where('auth_name', '<>', '')->orderBy('sort')->get(); //父级
            $auth_infoB = Auth::where('level', '1')->where('auth_name', '<>', '')->orderBy('sort')->get(); //子级1
            $auth_infoC = Auth::where('level', '2')->where('auth_name', '<>', '')->orderBy('sort')->get(); //子级2

            return view('admin.role.add', compact('auth_infoA', 'auth_infoB', 'auth_infoC'));
        }
    }

    //角色删除
    public function del(Request $request)
    {

        $id = $request->id;
        if ($id <= 3 || $id == 50) {
            //ID 为1,2,3的角色，禁止删除
            return '0';
        }

        //  delete 返回受影响的行数
        $result = Role::where('id', $id)->delete();
        //由于框架自身不支持响应bool值，所以转化一种形式
        return $result >= 0 ? '1' : '0';
    }
}
